/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bgu.sonar.util.async;

import bgu.sonar.util.ui.mon.Monitor;
import bgu.sonar.util.ui.mon.SimpleMonitor;

/**
 *
 * @author bennyl
 */
public abstract class MonitoredAsyncTask<T> extends AsyncTask<T> {

    Monitor monitor = null;

    public MonitoredAsyncTask(String name) {
        monitor = new SimpleMonitor(name);
        super.getResult().addCallback(new TerminationCallback() {
            @Override
            public void onTrminated(boolean success) {
                if (monitor != null) {
                    monitor.close();
                }
            }
        });
    }

    public Monitor getMonitor() {
        return monitor;
    }

    public MonitoredAsyncTask<T> setMonitor(Monitor monitor) {
        this.monitor = monitor;
        return this;
    }

    protected void print(String what) {
        if (this.monitor != null) {
            monitor.write(what);
        }
    }

    protected void println(String what) {
        if (this.monitor != null) {
            monitor.write(what + "\n");
        }
    }

    protected void progress(double progress) {
        if (this.monitor != null) {
            monitor.incProgress(progress);
        }
    }

    protected void resetProgressTo(double progress) {
        if (this.monitor != null) {
            monitor.setProgress(progress);
        }
    }
}
